package com.hk.webapp.service.impl;

import com.hk.webapp.bean.User;
import com.hk.webapp.bo.UserBo;
import com.hk.webapp.bo.XcxUserInfoBo;
import com.hk.webapp.dao.UserMapper;
import com.hk.webapp.dao.custom.CUserMapper;
import com.hk.webapp.error.BusinessErrorEnum;
import com.hk.webapp.error.BusinessException;
import com.hk.webapp.res_config.JwtAudience;
import com.hk.webapp.utils.JwtUtil;
import com.hk.webapp.utils.encryptions.AESUtils;
import com.hk.webapp.vo.*;
import com.hk.webapp.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Resource
    private CUserMapper cUserMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private JwtAudience audience;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo findUsableUser(LoginVo loginVo) {
        UserBo userBo = cUserMapper.selectUsableUser(loginVo.getUser_name());
        try {
            Integer isLock = userBo.getIsLock();
            if (isLock == 1) {
                return ResultVo.createByErrorEnum(BusinessErrorEnum.USER_ACCOUNT_LOCKED);
            }
            String passwd = loginVo.getPasswd();
            String encryptPwd = AESUtils.encrypt(passwd, userBo.getSalt());
            if (!encryptPwd.equals(userBo.getStorePwd())) {
                return ResultVo.createByErrorEnum(BusinessErrorEnum.USER_PASSWORD_ERROR);
            }
        } catch (Exception e) {
            return ResultVo.createByErrorEnum(BusinessErrorEnum.USER_PASSWORD_ERROR);
        }
        return ResultVo.createBySuccessData(userBo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo findUsableUser02(Login02Vo vo) {
        UserBo userBo = cUserMapper.selectUsableUser(vo.getUser_name());
        try {
            Integer isLock = userBo.getIsLock();
            if (isLock == 1) {
                return ResultVo.createByErrorEnum(BusinessErrorEnum.USER_ACCOUNT_LOCKED);
            }
            String passwd = vo.getPasswd();
            String encryptPwd = AESUtils.encrypt(passwd, userBo.getSalt());
            if (!encryptPwd.equals(userBo.getStorePwd())) {
                   return ResultVo.createByErrorEnum(BusinessErrorEnum.USER_PASSWORD_ERROR);
            }
        } catch (Exception e) {
            return ResultVo.createByErrorEnum(BusinessErrorEnum.USER_PASSWORD_ERROR);
        }
        return ResultVo.createBySuccessData(userBo);
    }

    @Override
    public Integer getCount() {
        return userMapper.getCount();
    }

    @Override
    public PageInfo<User> getSearchPageList(KeywordPageVo vo) {
        PageHelper.startPage(vo.getPageIndex(), vo.getPageSize());
        List<User> list = userMapper.getSearchPageList(vo);
        PageInfo<User> plist = new PageInfo<>(list);
        return plist;
    }

    @Override
    public User findUserByOpenId(String openId) {
        return userMapper.selectUserByOpenId(openId);
    }

    @Override
    @Transactional
    public String createUser(XcxUserInfoBo xcxUserInfoBo) throws BusinessException {
        User user = findUserByOpenId(xcxUserInfoBo.getOpenId());
        if (Objects.isNull(user)) {
            user = xcxUserInfoBo.convertXcxUserInfoBo2User();
            int insertMark = userMapper.insertSelective(user);
            if (insertMark != 1) {
                return StringUtils.EMPTY;
            }
        } else {
            User updateUser = xcxUserInfoBo.convertXcxUserInfoBo2User();
            updateUser.setId(user.getId());
            int updateMark = userMapper.updateByPrimaryKeySelective(updateUser);
            if (updateMark != 1) {
                return StringUtils.EMPTY;
            }
        }
        return JwtUtil.generateToken(audience, user.getId(), user.getWxNickname(), false);
    }

    @Override
    public User selectById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    @Transactional
    public int changeUserType(Integer userId, Integer type) {
        return userMapper.changeUserType(userId,type);
    }

    @Override
    @Transactional
    public int changeOnlineStatus(Integer userId, Integer onlineStatus) {
        return userMapper.changeOnlineStatus(userId,onlineStatus);
    }

    @Override
    public List<NameValueVo> getUserProvinceCounts() {
        return userMapper.getUserProvinceCounts();
    }

    @Override
    public List<NameValueVo> getStatistical() {
        return userMapper.getStatistical();
    }
}
